#ifndef STACK
#define STACK
template<typename TContainer>
class Stack
{
public:
	using ValueType = typename TContainer::ValueType;
	using ExceptionType = int;
	static constexpr int NoCapacityLimit = -1;
	static constexpr ExceptionType StackEmpty = -1;
	static constexpr ExceptionType StackFull = -2;
public:
	// 建立一个指定最大容量的空栈，默认为无限制
	Stack(int capacity = NoCapacityLimit);
	// 禁用拷贝和移动构造
	Stack(const Stack&) = delete;
	Stack(Stack&&) = delete;
	~Stack();
	// 获取栈内元素数量
	int Count();
	// 出栈并返回出栈的元素
	ValueType Pop();
	// 返回栈顶元素的引用
	ValueType& Peak();
	// 入栈
	void Push(const ValueType& value);

private:
	TContainer* pContainer;
	int capacity;
};

template<typename TContainer>
Stack<TContainer>::Stack(int capacity)
{
	this->capacity = capacity;
	this->pContainer = new TContainer();
}

template<typename TContainer>
Stack<TContainer>::~Stack()
{
	delete this->pContainer;
}

template<typename TContainer>
int Stack<TContainer>::Count()
{
	return this->pContainer->Count();
}

template<typename TContainer>
typename Stack<TContainer>::ValueType Stack<TContainer>::Pop()
{
	if (this->Count() == 0)
	{
		throw StackEmpty;
	}
	ValueType value = *(this->pContainer->ReversedBegin());
	this->pContainer->Remove(this->pContainer->ReversedBegin());
	return value;
}

template<typename TContainer>
typename Stack<TContainer>::ValueType& Stack<TContainer>::Peak()
{
	if (this->Count() == 0)
	{
		throw StackEmpty;
	}
	return *(this->pContainer->ReversedBegin());
}

template<typename TContainer>
void Stack<TContainer>::Push(const ValueType& value)
{
	if (this->Count() == this->capacity)
	{
		throw StackFull;
	}
	this->pContainer->Insert(value, this->pContainer->End());
}

#endif // !STACK


